package com.blog.controller;


import com.blog.common.constant.ArticleCategory;
import com.blog.common.constant.ArticleTag;
import com.blog.common.constant.ResultCode;
import com.blog.dto.PageDto;
import org.apache.ibatis.annotations.Delete;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import com.blog.common.result.Result;
import com.blog.entity.Article;
import com.blog.service.ArticleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;


@RestController
@RequestMapping(value = "/articles")
@Api(tags = "文章接口")
public class ArticleController {

    private static final Logger log = LoggerFactory.getLogger(ArticleController.class);
    @Autowired
    private ArticleService articleService;

    @GetMapping
    @ApiOperation(value = "获取文章列表", notes = "获取所有的文章列表")
    public Result<List<Article>> listArticles(
            @ApiParam(value = "文章查询条件") Article article) {
        Result<List<Article>> articles = articleService.findArticles(article);
        if (articles.getData().isEmpty()) {
            return Result.error(ResultCode.NOT_FOUND);
        }
        return articles;
    }

    @GetMapping("/list")
    public Result listAll(){
        return articleService.listAll();
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID获取文章", notes = "根据文章ID获取文章详情")
    public Result getArticleById(
            @ApiParam(value = "文章ID", required = true) @PathVariable("id") Integer id) {

            return articleService.getArticleById(id);
    }


    @GetMapping("/updateGetDetail/{id}")
    @ApiOperation(value = "根据ID获取文章", notes = "根据文章ID获取文章详情")
    public Result getArticleById2(
            @ApiParam(value = "文章ID", required = true) @PathVariable("id") Integer id) {
        System.out.println("id:" + id);
        Result result = articleService.getArticleById(id);
        //articleService.deleteArticleById(id);
        return result;
    }

    @GetMapping("/tag/{id}")
    @ApiOperation(value = "根据标签获取文章", notes = "根据标签ID获取文章列表")
    public Result listArticlesByTag(
            @ApiParam(value = "标签ID", required = true) @PathVariable("id") Integer id) {
        return articleService.findArticlesByTag(id);
    }

    @GetMapping("/category/{categoryId}")
    @ApiOperation(value = "根据分类获取文章", notes = "根据分类ID获取文章列表")
    public Result listArticlesByCategory(
            @ApiParam(value = "分类ID", required = true) @PathVariable("categoryId") Integer categoryId) {
        Result<List<Article>> articles = articleService.findArticlesByCategory(categoryId);
        return articles;
    }

    @RequestMapping("/publish")
    @ApiOperation(value = "发布文章", notes = "发布新的文章")
    public Result saveArticle(
            @RequestParam("title") String title,
            @RequestParam("content") String content,
            @RequestParam("tag") String tag,
            @RequestParam("category") String category,
            @RequestParam("account") String account
    ){
        Integer tagI=null;
        Integer categoryI=null;
        if (tag.equals(ArticleTag.JAVA.getDescription()) ){
            tagI=ArticleTag.JAVA.getCode();
        } else if (tag.equals(ArticleTag.PROJECT.getDescription())) {
            tagI=ArticleTag.PROJECT.getCode();
        } else {
            tagI=ArticleTag.FUNDAMENTAL.getCode();
        }
        if (category.equals(ArticleCategory.FRONT_END.getDescription())){
            categoryI=ArticleCategory.FRONT_END.getCode();
        } else if (category.equals(ArticleCategory.BACK_END.getDescription())) {
            categoryI=ArticleCategory.BACK_END.getCode();
        }else {
            categoryI=ArticleCategory.DATABASE.getCode();
        }


        Result result = articleService.saveArticle(title, content, tagI, categoryI,account);
        return result;
    }


    @PostMapping("/update")
    @ApiOperation(value = "更新文章", notes = "更新已有的文章")
    public Result updateArticle(
            @ApiParam(value = "文章信息", required = true) @Validated @RequestBody Article article) {
        articleService.updateArticle(article);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除文章", notes = "根据文章ID删除文章")
    public Result deleteArticleById(
            @ApiParam(value = "文章ID", required = true) @PathVariable("id") Integer id) {
        articleService.deleteArticleById(id);
        return Result.success();
    }
}
